package xyz.riceball.develop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.develop.entity.po.DictionaryPO;
import xyz.riceball.develop.service.DictionaryService;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.ApiRc;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.framework.core.valid.ValidAdd;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 字典表(Dictionary)表控制层
 *
 * @author xiaovcloud
 * @since 2022-04-26 13:11:27
 */
@Api(tags = "字典管理")
@RestController
@RbSysRequest
@RequestMapping("/rb/api/develop/dictionary")
public class DictionaryController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private DictionaryService dictionaryService;

    // ------------------ 以下是字典分类接口 ------------------
    /**
     * 分页查询字典分类
     *
     * @param pageParam
     * @param dictionary
     * @return
     */
    @ApiOperation("分页查询字典分类")
    @GetMapping
    @RbLog(description = "分页查询字典分类")
    public ApiR pageCategory(PageParam pageParam, DictionaryPO dictionary) {
        return success(this.dictionaryService.pageCategory(PageParam.of(pageParam), dictionary));
    }

    /**
     * 新增字典分类
     *
     * @param dictionary 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增字典分类")
    @PostMapping
    @RbLog(description = "新增字典分类")
    public ApiR<DictionaryPO> insertCategory(@RequestBody @Valid @Validated(ValidAdd.class) DictionaryPO dictionary) {
        return dictionaryService.addCategory(dictionary);
    }

    /**
     * 修改数据
     *
     * @param dictionary 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改字典分类")
    @PutMapping
    @RbLog(description = "修改字典分类")
    public ApiR update(@RequestBody DictionaryPO dictionary) {
        boolean update = dictionaryService.edit(dictionary);
        return ApiR.msg(update, "修改字典成功", "修改字典失败");
    }

    /**
     * 删除字典类别
     *
     * @param codeList 字典类别使用code删除
     * @return 删除结果
     */
    @ApiOperation("删除字典类别")
    @DeleteMapping
    @RbLog(description = "删除字典类别")
    public ApiR delete(@RequestBody List<String> codeList) {
        return dictionaryService.delete(codeList);
    }

    // ------------------ 以下是字典项接口 ------------------
    /**
     * 分页查询字典项
     *
     * @param page
     * @param dictionary
     * @return
     */
    @ApiOperation("分页查询字典项")
    @GetMapping("/item")
    @RbLog(description = "分页查询字典项")
    public ApiR<Page<DictionaryPO>> pageItem(PageParam page, DictionaryPO dictionary) {
        return success(this.dictionaryService.pageItem(PageParam.of(page), dictionary));
    }

    /**
     * 新增字典项
     *
     * @param dictionary 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增字典项")
    @PostMapping({"/item"})
    @RbLog(description = "新增字典项")
    public ApiR<DictionaryPO> insertItem(@RequestBody DictionaryPO dictionary) {
        return dictionaryService.addItem(dictionary);
    }

    /**
     * 修改字典项
     *
     * @param dictionary 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改字典项")
    @PutMapping("item")
    @RbLog(description = "修改字典项")
    public ApiR updateItem(@RequestBody DictionaryPO dictionary) {
        boolean update = dictionaryService.editItem(dictionary);
        return ApiR.msg(update, "修改字典成功", "修改字典失败");
    }

    /**
     * 删除字典项
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除字典项")
    @DeleteMapping("/item")
    @RbLog(description = "删除字典项")
    public ApiRc<Void> deleteItem(@RequestBody List<String> ids) {
       return dictionaryService.deleteItem(ids);
    }

    /**
     * 根据codes查询字典
     * @param codes
     * @return
     */
    @ApiOperation("根据codes查询字典项集合")
    @GetMapping("/item/codes")
    @RbLog(description = "根据codes查询字典项集合")
    public ApiR<List<List<DictionaryPO>>> selectItem(@RequestParam("codes") List<String> codes) {
        return success(this.dictionaryService.selectItem(codes));
    }

    /**
     * 查询code下的value
     *
     * @param code
     * @param values
     * @return
     */
    @ApiOperation("根据字典的code和value查询字典项")
    @GetMapping("/item/code/{code}/values")
    @RbLog(description = "根据字典的code和value查询字典项")
    public ApiR<List<DictionaryPO>> selectItem(@PathVariable String code,
                                               @RequestParam("values") List<String> values) {
        return success(this.dictionaryService.selectItem(code, values));
    }

}

